/**
 * 注册校验
 * @authors gaocc
 * @date    2014-09-17 09:01:42
 * @update  2014-09-17 09:01:42
 * @for change_password.jsp
 * @version $Id$
 */
var BSTYUN = BSTYUN || {};
//全局弹窗
BSTYUN.check = {
    //IE6判定
    isIE6: window.VBArray && !window.XMLHttpRequest,
    //判断是否为中英文，数字，下划线，减号
    isNick: function(str) {
        var nickReg = /^[\u4e00-\u9fa5A-Za-z0-9-_]+$/;
        return nickReg.test(str);
    },
//    //判断邮箱
//    isEmail: function(str) {
//        var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
//        return emailReg.test(str);
//    },
    //判断手机.
    isMobile: function(str) {
        var mobileReg = /^1[345678][0-9]{9}$/;
        return mobileReg.test(str);
    },
    // 判断固话
    isTelephone: function(str) {
        var phoneReg = /^0\d{2,3}-\d{5,9}$/;
        return phoneReg.test(str);
    },
    //判断URL
    isUrl: function(str) {
        var urlReg = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$/;
        return urlReg.test(str);
    },
    //判断数字
    isNum: function(str) {
        var numReg = /^[0-9]\d*$/;
        return numReg.test(str);
    }
};
BSTYUN.changePassword = {
    $inputs: null,
    flag: {password: false, newPassword: false, checkPassword: false},
    init:function() {
        var that = this;
        /*获取input元素*/
        that.$inputs = $('.input_box').find('input');
        /*检测信息*/
        that.checkInfor();
        /*密码修改提交*/
        that.submitChangePassword();
    },
    checkInfor:function() {
        var that = this;
        /*获取焦点，清空错误信息*/
        that.$inputs.on('focus', function(event) {
            that.clearErrorMsg($(this));
            /*失去焦点，检测信息正确性*/
        }).on('blur', function(event) {
            var $this = $(this),
                typename = $this.attr('name'),
                val = $.trim($this.val());
            /*输入内容为空，则提示不能为空*/
            if(val == '' || val.length == 0){
                that.addErrorMsg($this,'不能为空');
                return false;
            }
            if(typename == 'password'){
            	 $.ajax({
                     type: "post",
                     url: "/user/changePassword?random="+Math.random(),
                     data: {password:val},
                     async:false,
                     success: function(data){
                       if(data==false){
                           that.addErrorMsg($this,"原始密码不正确");
                       }
                    }
                 });
            }else if (typename == 'newPassword') {
                var $checkPassword = $this.parents('.input_box').next().find('#checkPassword'),
                    cfm_psd_val = $.trim($checkPassword.val());
                if (val.length<8 || val.length >16) {
                    that.addErrorMsg($this,'长度必须在8-16之间');
                }
                if (cfm_psd_val != '' || cfm_psd_val.length != 0) {
                    if (val != cfm_psd_val) {
                        that.addErrorMsg($checkPassword,'两次输入密码不一致');
                    }else{
                        that.clearErrorMsg($checkPassword);
                    };
                };
            }else if (typename == 'checkPassword') {
                var psd = $.trim($('#newPassword').val());
                if (val != psd) {
                    that.addErrorMsg($this,'两次输入密码不一致');
                };
            }
        });
    },
    submitChangePassword:function() {
        var that = this,
            $submit = $("#go2register");
        $submit.on('click', function(event) {
            var flag = that.flag,
                data = {};
            $.each(that.$inputs, function(index, val) {
                 $(this).trigger('blur');
            });
            if (flag.password != false&&flag.newPassword != false && flag.checkPassword != false) {
            	var password = $.trim(that.$inputs.eq(1).val());
                $.ajax({
                    type: "POST",
                    url : "/user/updatePassword?random="+Math.random(),
                    data: {password:password},
                    async: false,
                    error: function(request) {
                    	BSTYUN.dialog.warn('服务器繁忙，请稍后尝试')
                        return false;
                    },
                    success: function(data) {
                        if(data==true){
                        	BSTYUN.dialog.ok('密码修改成功，请重新登录！');
                        	$(".inputtext").val("");
                        }else{
                        	BSTYUN.dialog.warn('密码修改失败，请稍后再试！');
                            return false;
                        }
                    }
                });
            };
            return false;
        });
    },
    /*清空$this的错误提示信息*/
    clearErrorMsg:function($this) {
        var $this = $this,
            that = this,
            typename = $this.attr('name');
        if (typename == 'password') {
            that.flag.password = true;
            $this.parent().next().children('.error_msg').remove();
        }else if (typename == 'newPassword') {
            that.flag.newPassword = true;
            $this.parent().next().children('.error_msg').remove();
        }else if (typename == 'checkPassword') {
            that.flag.checkPassword = true;
            $this.parent().next().children('.error_msg').remove();
        }
    },
    /*显示错误信息*/
    addErrorMsg:function ($this,msg) {
        var $this = $this,
            that = this,
            typename = $this.attr('name');
        if (typename == 'password') {
            that.flag.password = false;
            $this.parent().next().html('<span class="error_msg">'+msg+'</span>');
        }else if (typename == 'newPassword') {
            that.flag.newPassword = false;
            $this.parent().next().html('<span class="error_msg">'+msg+'</span>');
        }else if (typename == 'checkPassword') {
            that.flag.checkPassword = false;
            $this.parent().next().html('<span class="error_msg">'+msg+'</span>');
        }
    },
}
$(function() {
    BSTYUN.changePassword.init();
});

/**
 * 返回安全信息页面
 */
function returnSafePage(){
	window.location.href="/user/userCenter/memberSafetyInfo";
}

